﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class xiaoshoujia : System.Web.UI.Page
{
    private static string sqlconn = System.Configuration.ConfigurationManager.AppSettings["sqlcon"].ToString().Trim();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Binddate();
            gvxiaoshou.Visible = false;
        }
    }
    protected void Binddate()
    {
        users us = new users();
        gvxiaoshou.DataSource = us.GetAllxiaoshou(us);
        gvxiaoshou.DataBind();
    }
    protected void btnjia_Click(object sender, EventArgs e)
    {
        if (txtdh.Text != "" && txtyid.Text != "" && txtyname.Text != "" && txtyshu.Text != "" && txtypri.Text != "" && txttname.Text != "" && txtze.Text != "" && txttime.Text != "")
        {
            string dh = txtdh.Text.Trim();
            string time = txttime.Text.Trim();
            string yid = txtyid.Text.Trim();
            string yname = txtyname.Text.Trim();
            string number = txtyshu.Text.Trim();
            string price = txtypri.Text.Trim();
            string tname = txttname.Text.Trim();
            string ze = txtze.Text.Trim();
            users us = new users();
            us.dh = dh;
            us.time = time;
            us.id = yid;
            us.name = yname;
            us.number = number;
            us.price = price;
            us.ze = ze;
            us.tname = tname;

            SqlDataReader rd = us.getkun(us);
            while (rd.Read())
            {
                string num = rd["K_NUMBER"].ToString();

                int n = Convert.ToInt32(num);

                if (n > 0)
                {
                    int sl = Convert.ToInt32(txtyshu.Text.ToString().Trim());
                    if (n >= sl)
                    {
                        n -= sl;

                        us.numb = Convert.ToString(n);
                        int sum = us.Updakc(us);


                        int result = us.addxiaoshou(us);
                        if (result != 0)
                        {
                            Response.Write("<script>alert(\"添加记录成功！\")</script>");
                            gvxiaoshou.Visible = true;
                            Binddate();
                        }

                    }
                    else
                    {
                        Response.Write("<script>alert(\"数量不足！\")</script>");
                    }
                }
                else
                {
                    Response.Write("<script>alert(\"缺货！\")</script>");
                }
            }

        }
        else
        {
            Response.Write("<script>alert(\"请输入信息！\")</script>");
        }

    }
    protected void btncin_Click(object sender, EventArgs e)
    {
        txtdh.Text = null;
        txttime.Text = null;
        txtyid.Text = null;
        txtyname.Text = null;
        txtyshu.Text = null;
        txtypri.Text = null;
        txttname.Text = null;
        txtze.Text = null;
    }
    protected void gvxiaoshou_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvxiaoshou.PageIndex = e.NewPageIndex;
        Binddate();
    }
    protected void txtyid_TextChanged(object sender, EventArgs e)
    {
        if (txtyid.Text != "")
        {
            users us = new users();
            int result = us.kucun(us);
            if (result != 0)
            {
                string NowYear = DateTime.Now.ToString("yyy");
                string NowMouth = DateTime.Now.ToString("MM");
                string NowDay = DateTime.Now.ToString("dd");
                if (NowMouth.Length == 1)
                {
                    NowMouth = "0" + NowMouth;
                }
                if (NowDay.Length == 1)
                {
                    NowDay = "0" + NowDay;
                }
                string NowTime = NowMouth + '/' + NowDay + '/' + NowYear;


                us.youxiaoqi = NowTime;

                int da = us.getkcg(us);
                if (da != 0)
                {
                    Label2.Text = "药品已过期";
                    Label2.Visible = true;
                }
            }

        }
    }
}